package cn.com.utt.unms.mapper;

import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import cn.com.utt.unms.pojo.Group;
import cn.com.utt.unms.vo.GroupToNetwork;

@Mapper
public interface GroupMapper {
	
	@Select("select * from `group` g where pId=#{pId}")
	List<Group> queryByPid(@Param("pId")long pId);
	
	@Select("select g.id g_id,g.pId g_pid,g.name g_name,n.id n_id,n.name n_name,n.group_id n_pid from `group` g left join network n on n.group_id = g.id where g.pId =#{pId}")
	List<GroupToNetwork> queryByPidContainsNetwork(@Param("pId")long pId);
	
	@Insert("insert into `group`(name,remark,pId) values(#{name},#{remark},#{pId})")
	@Options(useGeneratedKeys=true, keyProperty="id", keyColumn="id")
	void save(Group group);
	
	@Update("update `group` set name=#{name} where id=#{id}")
	void update(Group group);
	
	@Delete({"<script>",
	    "delete ", 
	    "FROM `group`",
	    "WHERE ", 
	    "id IN", 
	      "<foreach item='item' index='index' collection='groupIds'",
	        "open='(' separator=',' close=')'>",
	        "#{item}",
	      "</foreach>",
	"</script>"})
	void delete(@Param("groupIds")List<Long> groupIds);
	
	@Select("select * from `group` where id=#{id}")
	Group queryById(@Param("id")long id);
	
	@Select("select g.id g_id,g.pId g_pid,g.name g_name,n.id n_id,n.name n_name,n.group_id n_pid from `group` g left join network n on n.group_id = g.id where g.id =#{id}")
	List<GroupToNetwork> queryByIdContainsNetwork(@Param("id")long id);
	
}
